<template>
  <div>
    <el-button type="primary" @click="submitData">确 定</el-button>
    <el-button type="danger" @click="batchDelete">批量删除</el-button>

    <el-tree
      :data="menus"
      :props="defaultProps"
      node-key="catId"
      show-checkbox
      ref="menuTree"
      :expand-on-click-node="false"
      :default-expanded-keys="expandedkey"
    >
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
            v-if="node.level <= 2"
            type="text"
            size="mini"
            @click="() => append(data)"
          >
            Append
          </el-button>
          <el-button type="text" size="mini" @click="edit(data)">
            Edit
          </el-button>
          <el-button
            v-if="node.childNodes.length == 0"
            type="text"
            size="mini"
            @click="() => remove(node, data)"
          >
            Delete
          </el-button>
        </span>
      </span>
    </el-tree>

    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="category">
        <el-form-item label="分类名称">
          <el-input v-model="category.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-input v-model="category.icon" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="计量单位">
          <el-input
            v-model="category.productUnit"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitData">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      title: "",
      dialogType: "",
      dialogFormVisible: false,
      category: {
        name: "",
        parentCid: 0,
        catLevel: 0,
        showStatus: 1,
        sort: 0,
        catId: null,
        icon: "",
        productUnit: "",
      },
      expandedkey: [],
      menus: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
    };
  },
  methods: {
    //批量删除的方法
    batchDelete() {
      let ids = [];
      let checkedNodes = this.$refs.menuTree.getCheckedNodes();
      checkedNodes.forEach((element) => {
        ids.push(element.catId);
      });
      this.$confirm("确认删除已选中菜单？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/product/category/delete"),
            method: "post",
            data: this.$http.adornData(ids, false),
          }).then(({ data }) => {
            this.getMenus();
            this.$message({
              message: "删除成功！",
              type: "success",
            });
          });
        })
        .catch(() => {});
    },

    // 获取数据列表
    getMenus() {
      this.$http({
        url: this.$http.adornUrl("/product/category/list/tree"),
        method: "get",
      }).then(({ data }) => {
        this.menus = data.data;
      });
    },

    submitData() {
      if (this.dialogType == "add") {
        this.addCategory();
      }
      if (this.dialogType == "edit") {
        this.editCategory();
      }
    },

    append(data) {
      console.log("append的数据---->", data);
      this.title = "新增";
      this.dialogFormVisible = true;
      this.dialogType = "add";
      this.category.parentCid = data.catId;
      this.category.catLevel = data.catLevel * 1 + 1;
      this.category.icon = "";
      this.category.productUnit = "";
      this.category.name = "";
    },

    addCategory() {
      console.log("新增加的数据---->", this.category);
      this.$http({
        url: this.$http.adornUrl("/product/category/save"),
        method: "post",
        data: this.$http.adornData(this.category, false),
      }).then(({ data }) => {
        this.$message({
          message: "保存成功！",
          type: "success",
        });
        this.dialogFormVisible = false;
        this.getMenus();
        this.expandedkey = [this.category.parentCid];
      });
    },
    // 修改按钮方法
    edit(data) {
      console.log("edit的数据---->", data);
      this.title = "修改";
      this.dialogFormVisible = true;
      this.dialogType = "edit";
      // 查数据库尽心回显
      this.$http({
        url: this.$http.adornUrl(`/product/category/info/${data.catId}`),
        method: "get",
      }).then(({ data }) => {
        console.log("data------->", data);
        this.category.name = data.data.name;
        this.category.catId = data.data.catId;
        this.category.icon = data.data.icon;
        this.category.productUnit = data.data.productUnit;
        this.category.parentCid = data.data.parentCid;
      });
    },
    // 真正的修改方法
    editCategory() {
      var { name, catId, icon, productUnit } = this.category;
      var editdata = { name, catId, icon, productUnit };
      this.$http({
        url: this.$http.adornUrl("/product/category/update"),
        method: "post",
        data: this.$http.adornData(editdata, false),
      }).then(({ data }) => {
        this.$message({
          message: "修改成功！",
          type: "success",
        });
        this.dialogFormVisible = false;
        this.getMenus();
        this.expandedkey = [this.category.parentCid];
      });
    },

    remove(node, data) {
      var ids = [data.catId];

      this.$confirm(`确认删除【${data.name}】菜单？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/product/category/delete"),
            method: "post",
            data: this.$http.adornData(ids, false),
          }).then(({ data }) => {
            this.getMenus();
            this.$message({
              message: "删除成功！",
              type: "success",
            });
            // 展开刚刚展开的节点
            this.expandedkey = [node.parent.data.catId];
          });
        })
        .catch(() => {});
    },
  },
  created() {
    this.getMenus();
  },
};
</script>

<style>
</style>